<template>

  <div class="app-container">
    <el-form
      :model="hospitalSet"
      :rules="rules"
      ref="hospitalSet"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="医院名称" prop="hosname">
        <el-input v-model="hospitalSet.hosname"></el-input>
      </el-form-item>
      <el-form-item label="医院编号" prop="hoscode">
        <el-input v-model="hospitalSet.hoscode"></el-input>
      </el-form-item>
      <el-form-item label="api基础路径" prop="apiUrl">
        <el-input v-model="hospitalSet.apiUrl"></el-input>
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactsName">
        <el-input v-model="hospitalSet.contactsName"></el-input>
      </el-form-item>
      <el-form-item label="联系人手机" prop="contactsPhone">
        <el-input v-model="hospitalSet.contactsPhone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUptate('hospitalSet')"
          >保存</el-button
        >
      </el-form-item>
    </el-form>

  </div>
</template>


<script>
import hospset from "@/api/hospset";
export default {
  data() {
    return {
      //添加数据
      hospitalSet: {
        hosname: "",
        hoscode: "",
        apiUrl: "",
        contactsName: "",
        contactsPhone: "",
      },
      //表单验证
      rules: {
        hosname: [
          { required: true, message: "请输入医院名称", trigger: "blur" },
        ],
        hoscode: [
          { required: true, message: "请输入医院编号", trigger: "blur" },
        ],
        apiUrl: [
          { required: true, message: "请输入api基础路径", trigger: "blur" },
        ],
        contactsName: [
          { required: true, message: "请输入联系人姓名", trigger: "blur" },
        ],
        contactsPhone: [
          { required: true, message: "请输入联系人手机", trigger: "blur" },
          {
            pattern: /0?(13|14|15|18|17)[0-9]{9}/,
            message: "请输入正确的号码格式",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    //获取路由里的id值
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id;
      //调用结构得到医院设置信息
      this.getHospital(id);
    }
  },
  methods: {
    //新增或修改医院设置
    saveOrUptate(formName) {
      //表单验证
      this.$refs[formName].validate((valid) => {
        if (valid) {
    
          //判断有没有携带参数并且参数里有没有id参数如果有则调用修改方法否则调用增加方法
          if (this.$route.params && this.$route.params.id) {
            this.update();
          } else {
            this.save();
          }
        } else {
          return false;
        }
      });
    },
    //获取指定医院设置
    getHospital(id) {
      hospset.getHospital(id).then((response) => {
        console.log(response);
        this.hospitalSet = response.data;
      });
    },
    //修改方法
    update() {
      hospset.updateHospitalSet(this.hospitalSet).then((response) => {
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //路由跳转到列表页面
        this.$router.push({ path: "/hospSet/list" });
      });
    },
     //新增方法
    save() {
      hospset.saveHospitalSet(this.hospitalSet).then((response) => {
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //路由跳转到列表页面
        this.$router.push({ path: "/hospSet/list" });
      });
    },
  },
};
</script>
